<!--
 * @Date: 2021-12-13 16:09:00
 * @LastEditors: LeeKing
 * @LastEditTime: 2021-12-22 17:51:14
 * @FilePath: \hrsaas\src\views\setting\index.vue
-->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="addRoleVisible = true"
            >新增角色</el-button>
            <el-table
              :data="tableData"
              style="width: 100%; margin: 15px 0"
              border
            >
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column label="角色名" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column
                label="操作"
              ><template slot-scope="{ row }">
                <el-button type="text">分配权限</el-button>
                <el-button type="text" @click="onEdit(row)">修改</el-button>
                <el-button type="text">删除</el-button>
              </template>
              </el-table-column>
            </el-table>

            <!-- //分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :span="8" :offset="3">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增角色弹层 -->

    <el-dialog
      title="新增角色"
      :visible.sync="addRoleVisible"
      width="50%"
      @close="addDialogClose"
    >
      <el-form
        ref="addRoleForm"
        :model="addRoleForm"
        label-width="80px"
        :rules="addRoleRules"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addRoleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo } from '@/api/company.js'
import { getRolesList, addRole, editRole } from '@/api/roles.js'
export default {
  data() {
    return {
      activeName: 'first',
      company: {},
      // 角色列表
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      total: 0,

      // 新增角色弹层
      addRoleVisible: false,
      addRoleForm: {},
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.loadCompanyInfo()
    this.loadRolesList()
  },

  methods: {
    // 获取公司信息
    async loadCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)

      this.company = res
    },
    // 获取角色信息列表
    async loadRolesList() {
      const res = await getRolesList(this.queryData)

      this.tableData = res.rows
      this.total = res.total
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.loadRolesList()
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.loadRolesList()
    },

    addDialogClose() {
      // 重置表单 和重置校验
      this.$refs.addRoleForm.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 表单提交
    async onSave() {
      await this.$refs.addRoleForm.validate()
      if (this.addRoleForm.id) {
        await editRole(this.addRoleForm)
        this.$message.success('更新成功！')
      } else {
        await addRole(this.addRoleForm)
        this.$message.success('添加成功！')
      }

      this.addRoleVisible = false
      this.loadRolesList()
    },
    // 修改
    onEdit(row) {
      this.addRoleVisible = true
      this.addRoleForm = { ...row }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-tabs__header {
  .el-tabs__item {
    width: 140px;
    text-align: center;
    font-size: 16px;
  }
}
</style>
